# -*- coding: utf-8 -*-
import json

import scrapy


class Spider1688Spider(scrapy.Spider):
    name = 'spider_1688'
    allowed_domains = ['detail.1688.com']
    # start_urls = ['https://detail.1688.com/offer/600053318697.html']
    start_urls = []

    def __init__(self, start_urls=None, **kwargs):
        super().__init__(**kwargs)  # python3
        self.start_urls = [start_urls]
        self.log(self.start_urls)

    def parse(self, response):
        show_img_list = response.xpath(
            "//ul[@class='nav nav-tabs fd-clr']/li/@data-imgs").extract()

        show_img_array = []
        for i_item in show_img_list:
            i_item = json.loads(i_item)
            format_img_url = self.format_img(i_item['original'], "60x60")
            show_img_array.append(format_img_url)
        print(show_img_array)

        color_img_list = response.xpath(
            "//ul[@class='list-leading']/li/div[@class='unit-detail-spec-operator']/@data-imgs").extract()
        color_img_array = []
        for i_item in color_img_list:
            i_item = json.loads(i_item)
            format_img_url = self.format_img(i_item['original'], "32x32")
            color_img_array.append(format_img_url)
        print(color_img_array)

    # https://cbu01.alicdn.com/img/ibank/2019/695/886/11655688596_848623286.jpg
    # https://cbu01.alicdn.com/img/ibank/2019/695/886/11655688596_848623286.32x32.jpg
    def format_img(self, url, format_str):
        arr = url.rsplit(".", 1)
        return arr[0] + '.' + format_str + '.' + arr[1]
